<?php
/** .-------------------------------------------------------------------
* |  Software: [HDCMS framework]
* |      Site: www.hdcms.com
* |-------------------------------------------------------------------
* |    Author: 向军 <2300071698@qq.com>
* |    WeChat: aihoudun
* | Copyright (c) 2012-2019, www.houdunwang.com. All Rights Reserved.
* '-------------------------------------------------------------------*/

namespace app\admin\controller;


class Goodslist extends Common
{
    private $db;

    public function __construct()
    {
        parent::__construct();
        $this->db = new \system\model\GoodsList();
    }


    //动作
    public function index()
    {
        $gid = Q('get.gid');
        //显示所有的规格名称
        $Data = Db::table('type_attr')->where('class', 1)->get();
        View::with('Data', $Data);
        //显示所有的货品
        $goodsListData = Db::table('goods_list')->where('gid', Q('gid'))->get();
        if ($goodsListData) {
            foreach ($goodsListData as $k => $v) {
                //将字符串转化成功数组
                $goodsListData[$k]['combine'] = explode(',', $v['combine']);
                foreach ($goodsListData[$k]['combine'] as $kk => $vv) {
                    $goodsListData[$k]['combine']['hh'][] = Db::table('goods_attr')
                        ->join('type_attr', 'goods_attr.taid', '=', 'type_attr.taid')
                        ->where('gtid', $vv)
                        ->field(['taname', 'gtvalue'])
                        ->get();
                }
            }
        }

        View::with('goodsListData', $goodsListData);

        //此处书写代码...
        return view();
    }
    //添加
    public function add(){

        if (IS_POST) {

            if ($this->db->store()) {
                message('添加成功', '', 'success');
            }
            message($this->db->getError(), '', 'error');
        }

        $gid = Q('gid');
        $Data = Db::table('type_attr')->where('class', 1)->get();
        foreach ($Data as $k => $v) {
            $Data[$k]['select'] = Db::table('goods_attr')
                ->where('taid', $v['taid'])
                ->where('gid', $gid)
                ->get();
        }
        View::with('Data', $Data);
        return view();
    }
    //删除
    public function del()
    {
        $glid = Q('u');
        $this->db->where('glid', $glid)->delete();
        //提示跳转
        message('删除成功', '', 'success');

    }

    public function edit(){
        if(IS_POST){
            if($this->db->edit()){
                message('编辑成功','','success');
            }
            message($this->db->getError(),'','error');
        }
        $gid = Q('gid');
        $glid=Q('glid');
        $oldData = Db::table('type_attr')->where('class', 1)->get();
        foreach ($oldData as $k => $v) {
            $oldData[$k]['select'] = Db::table('goods_attr')
                ->where('taid', $v['taid'])
                ->where('gid', $gid)
                ->get();
        }
        $goodslist=Db::table('goods_list')->where('glid',$glid)->first();
        View::with('goodslist',$goodslist);
        View::with('oldData', $oldData);
        return view();
    }
}
